from rest_framework import serializers

from .models import Addressee
import re


class AddresseeSerializer(serializers.Serializer):

	id = serializers.IntegerField(read_only=True)
	user = serializers.IntegerField(required=True, label='用户ID')
	email = serializers.EmailField(read_only=True, label='邮箱地址')
	mobile = serializers.CharField(read_only=True, label='手机号')

	class Meta:
		model = Addressee

	def validate_email(self, value):
		# print(value)
		if not re.match(r'^[0-9a-zA-Z_.-]+@[0-9a-zA-Z-]+(\\.[0-9a-zA-Z-]+)*\\.[0-9a-zA-Z-]{2,6}$', value):
			raise serializers.ValidationError('输入的邮箱格式不正确')
		return value
